 <?php
class Types extends Model_Abstract
{
	public $tableName = 'types';
	
	
	protected $_validation = array(
		'name' => array(
			array('max_length', '模板名称长度不能超过100', 100),
			array('not_empty', '请填写模板名称')
		),
		'name_en' => array(
			array('max_length', '模板名称(en)长度不能超过100', 100),
			array('not_empty', '请填写模板名称')
		),
		'tablename' => array(
			array('max_length', '表名长度不能超过100', 100),
			array('not_empty', '表名不能为空')
		),
		'columnname' => array(
			array('max_length', '列名长度不能超过100', 100),
			array('not_empty', '列名不能为空')
		),
		'color_hex' => array(
			array('max_length', '颜色Hex值的长度不能超过20', 20),
			array('not_empty', '颜色Hex值不能为空')
		),
		'is_default' => array(
			array('in_array', 'is_default只能设为0或1', array(0, 1))
		)
	);
	
	protected function _find($fields = '*', $attributes = array(), $conditions = array(), $params = array(), $constraint = array())
	{
		$result = parent::_find($fields, $attributes, $conditions, $params, $constraint);
		$c = array_merge(
			$this->_constraintFind,
			array('groupcolumnname' => false),
			$constraint
		);
		extract($c);
		
		if(!empty($result)){
			if($groupcolumnname){
				foreach($result as $k => $r){
					$result[$r['columnname']][] = $r;
					unset($result[$k]);
				}
			}
		}
		
		return $result;
	}
	
	public function init()
	{
		$this->initAttributes();
	}
	
	public static function model($classname = __CLASS__)
	{
		return parent::model($classname);
	}
	
	public function attributeLabels()
	{
		return array(
			'name' => '类型名称(Zh)',
			'name_en' => '类型名称(En)',
		);
	}

	protected function validate()
	{
		$ret = true;
		
		$ret2 = parent::validate();
		
		return ($ret && $ret2);
	}
	
	public function initAttributes()
	{
		$this->attributes = array(
			'name' => '',
			'name_en' => '',
			'tablename' => '',
			'columnname' => '',
			'color_hex' => '',
			'is_default' => 0
		);
	}
	
	/**
	 * @see Model_Abstract::_delete()
	 * 删除类别的同时，将删除的类别影响到的htmltemplates设为默认值
	 */
	protected function _delete($attributes = array(), $conditions = array(), $params = array())
	{
		$typeOne = $this->find('*', $attributes, $conditions, $params, array('onlyOne' => true));
		if(!empty($typeOne)){
			if(parent::_delete($attributes, $conditions, $params)){
				if($typeOne['tablename'] == 'htmltemplates'){
					$htmlTmeplatesModel = new Htmltemplates();
					
					$defaultType = $this->find('id', array('is_default' => 1, 'tablename' => 'htmltemplates', 'columnname' => $typeOne['columnname']), null, null, array('onlyOne' => true));
					if(!empty($defaultType)){
						$defaultId = $defaultType['id'];
					}else{
						$defaultId = 0;
					}
	
					$columnname = $typeOne['columnname'] . '_id';
					return $htmlTmeplatesModel->update(
						array($columnname => $defaultId), 
						array($columnname => $typeOne['id'])
					);
				}else if($typeOne['tablename'] == 'jqueryplugins'){
					$jqueryPluginsModel = new Jqueryplugins();
						
					$defaultType = $this->find('id', array('is_default' => 1, 'tablename' => 'jqueryplugins', 'columnname' => 'type_jq'), null, null, array('onlyOne' => true));
					if(!empty($defaultType)){
						$defaultId = $defaultType['id'];
					}else{
						$defaultId = 0;
					}
					
					$columnname = $typeOne['columnname'] . '_id';
					return $jqueryPluginsModel->update(
						array('type_id' => $defaultId),
						array('type_id' => $typeOne['id'])
					);
				}
			}
			return true;
		}
		
	}
	
	/**
	 * @param int $id
	 * 根据id设置is_default值为1，同时将htmltemplates中type_xxx_id为空或者0的记录，设其type_xxx_id为id
	 */
	public function setDefaultById($id)
	{
		$typeOne = $this->find('*', array('id' => $id), null, null, array('onlyOne' => true));
		if(!empty($typeOne)){
			$this->update(
				array('is_default' => 0), 
				array(
					'tablename' => $typeOne['tablename'], 
					'columnname' => $typeOne['columnname']
				)
			);
			$this->attributes = array(
				$this->primaryKey => $id,
				'is_default' => 1
			);
			if($this->save()){
				if($typeOne['tablename'] == 'htmltemplates'){
					$htmlTmeplatesModel = new Htmltemplates();
					$columnname = $typeOne['columnname'] . '_id';
					return $htmlTmeplatesModel->update(
						array($columnname => $id), 
						array($columnname => array(0, ''))
					);
				}else if($typeOne['tablename'] == 'jqueryplugins'){
					$jqueryPluginsModel = new Jqueryplugins();
					return $jqueryPluginsModel->update(
						array('type_id' => $id),
						array('type_id' => array(0, ''))
					);
				}
			}else{
				$this->_validationError['setDefaultById'][] = '设置失败';
				return false;
			}
		}else{
			$this->_validationError['setDefaultById'][] = '设置失败';
			return false;
		}
	}
	
	
	

}